package com.wcl.hencodertest.test09_meterialEditText;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;

import com.blankj.utilcode.util.ConvertUtils;
import com.wcl.hencodertest.R;

/**
 * @项目名： HenCoderTest
 * @包名： com.wcl.hencodertest.test09_meterialEditText
 * @文件名: MeterialEditText
 * @创建者: kilin
 * @创建时间: 2020/4/22 19:51
 * @描述： TODO
 */
public class MeterialEditText extends androidx.appcompat.widget.AppCompatEditText {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public static final int FLOATING_TEXT_SIZE = ConvertUtils.dp2px(12);
    public static final int FLOATING_MAGIN = ConvertUtils.dp2px(8);
    public static final int TEXT_VERTICAL_OFFSET = ConvertUtils.dp2px(22);
    public static final int TEXT_HORIZONTAL_OFFSET = ConvertUtils.dp2px(4);
    private static final int TEXT_ANIMATION_OFFSET = ConvertUtils.dp2px(20);


    private Rect rect = new Rect();

    private boolean useFloatingText;
    private boolean floatingLableShowing;
    private float floatingFraction;
    private ObjectAnimator animator;

    public MeterialEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        paint.setTextSize(FLOATING_TEXT_SIZE);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MeterialEditText);
        useFloatingText = typedArray.getBoolean(R.styleable.MeterialEditText_userFloatingText, true);
        typedArray.recycle();
        affterUseFloatingTextChange();
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (useFloatingText) {
                    if (TextUtils.isEmpty(charSequence)) {//自己写法
                        floatingLableShowing = false;
                        getObjectAnimator().reverse();
                    } else {
                        if (!floatingLableShowing) {
                            floatingLableShowing = true;
                            getObjectAnimator().start();
                        }
                    }

//                if (floatingLableShowing && TextUtils.isEmpty(charSequence)) {//老师写法
//                    floatingLableShowing = false;
//                    getObjectAnimator().reverse();
//                } else if (!floatingLableShowing && !TextUtils.isEmpty(charSequence)) {
//                    floatingLableShowing = true;
//                    getObjectAnimator().start();
//                }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private ObjectAnimator getObjectAnimator() {
        if (animator == null) {
            animator = ObjectAnimator.ofFloat(MeterialEditText.this, "floatingFraction", 0, 1);
        }
        return animator;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (useFloatingText) {
            getBackground().getPadding(rect);
            float verticalOffset = TEXT_VERTICAL_OFFSET + (1 - floatingFraction) * TEXT_ANIMATION_OFFSET;
            paint.setAlpha((int) (0xFF * floatingFraction));
            canvas.drawText(getHint().toString(), TEXT_HORIZONTAL_OFFSET, verticalOffset, paint);
        }
    }

    private void affterUseFloatingTextChange() {
        getBackground().getPadding(rect);
        if (useFloatingText) {
            setPadding(rect.left, rect.top + FLOATING_TEXT_SIZE + FLOATING_MAGIN, rect.right, rect.bottom);
            if (!TextUtils.isEmpty(getText())) {
                getObjectAnimator().start();
            }
        } else {
            setPadding(rect.left, rect.top, rect.right, rect.bottom);
            getObjectAnimator().reverse();
        }
    }

    public boolean isUseFloatingText() {
        return useFloatingText;
    }

    public void setUseFloatingText(boolean useFloatingText) {
        this.useFloatingText = useFloatingText;
        affterUseFloatingTextChange();
        invalidate();
    }

    public float getFloatingFraction() {
        return floatingFraction;
    }

    public void setFloatingFraction(float floatingFraction) {
        this.floatingFraction = floatingFraction;
        invalidate();
    }
}
